//
//  ZDWindController.m
//  特种车调度
//
//  Created by 陈宇 on 15/9/14.
//
//

#import "ZDWindController.h"
#import "ZDOrderDetailCell.h"
#import "CWStarRateView.h"
#import "BMAccountManager.h"
#import "ZDWorkDetailController.h"

@interface ZDWindController ()

@property (nonatomic, weak) UIView *topView;
@property (nonatomic, weak) ZDOrderDetailCell *detailCell;

#pragma mark 司机工作详情(工作中)(0)
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UIButton *closeOrderButton;

#pragma mark 司机取消订单(1)
@property (weak, nonatomic) IBOutlet UILabel *reasonLabel;

#pragma mark 待评价(2)
@property (weak, nonatomic) IBOutlet UILabel *signPriceLabel;   //签收金额
@property (weak, nonatomic) IBOutlet UILabel *signCountLabel;   //签收方量

@property (weak, nonatomic) IBOutlet UIButton *windButton;      //结算
@property (weak, nonatomic) IBOutlet UIButton *discussButton;      //结算
- (IBAction)windAction:(id)sender;

@property (weak, nonatomic) IBOutlet UIView *discussContainerView;
@property (weak, nonatomic) IBOutlet CWStarRateView *ratingView;
@property (weak, nonatomic) IBOutlet BMTextView *textView;

@end

@implementation ZDWindController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BMColor(245, 245, 245, 1);
    
    [self configuraion];
    
    [self setInfo];
}

- (void)setInfo
{
    double totalPrice = 0;
    NSInteger totalCount = 0;
    
    for (ServiceLog *log in _logs) {
        totalPrice += log.realsumPrice;
        totalCount += log.volume;
    }
    self.signPriceLabel.text = [NSString stringWithFormat:@"￥%.1f", totalPrice];
    self.signCountLabel.text = [NSString stringWithFormat:@"%ld方", totalCount];
}

- (void)configuraion {
    ZDOrderDetailCell *cell = [[NSBundle mainBundle] loadNibNamed:@"ZDOrderDetailCell" owner:nil options:nil].firstObject;
    cell.log = _log;
    cell.workStatusLabel.text = [cell.log getMsg];
    if (_order.status == ZDOrderStatusDoing || _order.status == ZDOrderStatusEnded) {
        cell.workStatusLabel.hidden = NO;
        if (cell.log.damage <= 0)
            cell.workStatusLabel.text = [cell.log getMsg];
        else
            cell.workStatusLabel.text = [NSString stringWithFormat:@"%@(获赔%.1f元)", [cell.log getMsg], cell.log.damage];
    } else if (_order.status == ZDOrderStatusCancel) {
        if (cell.log.statusId == 5 || cell.log.statusId == 11 || cell.log.statusId == 12) {
            //            cell.workStatusLabel.hidden = YES;
        } else {
            cell.workStatusLabel.hidden = NO;
            cell.workStatusLabel.text = [NSString stringWithFormat:@"获赔%.1f元", cell.log.damage];
        }
    } else {
        //        cell.workStatusLabel.hidden = YES;
    }
    
    UIView *containerView = [[UIView alloc] init];
    containerView.backgroundColor = [UIColor whiteColor];
    containerView.layer.shadowColor = [UIColor grayColor].CGColor;
    containerView.layer.shadowOpacity = 0.2;
    containerView.layer.shadowOffset = CGSizeMake(0, 2);
    [containerView addSubview:cell];
    [self.view addSubview:containerView];
    self.topView = containerView;
    self.detailCell = cell;
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(65.f);
        make.left.mas_equalTo(self.view).offset(10);
        make.right.mas_equalTo(self.view).offset(-10);
        make.top.mas_equalTo(self.view).offset(74);
    }];
    [cell mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(containerView).offset(0);
        make.height.mas_equalTo(57);
    }];
    
    UIView *windView = [[[NSBundle mainBundle] loadNibNamed:@"WorkStatusView" owner:self options:nil] lastObject];
    self.discussContainerView.hidden = YES;
    self.windButton.hidden = NO;
    [self.view addSubview:windView];
    [windView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).offset(10);
        make.right.mas_equalTo(self.view).offset(-10);
        make.top.mas_equalTo(self.topView.mas_bottom);
        make.bottom.mas_equalTo(self.view).offset(-10);
    }];
}

- (IBAction)windAction:(id)sender
{
    [self windTask];
}

- (void)windTask
{
    NSMutableString *ids = [NSMutableString string];
    
    for (ServiceLog *log in _logs) {
        [ids appendFormat:@"%ld,", log.ID];
    }
    
    BMRequest *request = [BMRequest requestWithPath:@"settlementServiceLogs.htm"];
    request.params = @{@"serviceId": [ids substringToIndex:ids.length - 1], @"disId": [BMAccountManager sharedInstance].user.disId, @"ownerId": @(_log.ownerId), @"truckId": @(_log.truckId)};
    [self showHudWithhint:kDefaultMessage];
    [[BMHttpTool sharedInstance] postWith:request finish:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == kResultOK) {
            for (ServiceLog *log in _logs) {
                log.status = 2;
            }
            NSArray *vcs = [self.navigationController childViewControllers];
            ZDWorkDetailController *detailVC = vcs[vcs.count - 2];
            [detailVC updateData];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [self showHint:@"结算失败!"];
        }
    }];
}

@end
